<ui:component xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:a4j="http://richfaces.org/a4j"
	xmlns:rich="http://richfaces.org/rich"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:o="http://openfaces.org">
	
	<h:form id="frmSelectLandUse">
		<h:panelGroup id="funCompleteSelection" rendered="#{not selectLandUseHelper.landUseNameInputFieldName.equals('')}">
			<script type="text/javascript">
				function completeSelection() {
					#{rich:element(selectLandUseHelper.landUseNameInputFieldName)}.value = '#{selectLandUseHelper.selectedNameStr}';
					//document.getElementById('#{selectLandUseHelper.landUseNameInputFieldName}').value = '#{selectLandUseHelper.selectedNameStr}';
					#{rich:component('selectLandUsePanel')}.hide();
				}
			</script>
		</h:panelGroup>
		
		<h:panelGroup id="frmLandUse">
			<rich:toolBar style="height:25px">
				<rich:toolBarGroup location="left">
					<h:outputText value="Nivel: #{selectLandUseHelper.currentLevel}" />
				</rich:toolBarGroup>
				<rich:toolBarGroup location="left">
				</rich:toolBarGroup>
				<rich:toolBarGroup location="right">
					<a4j:commandLink
						value="Nivel superior"
						action="#{selectLandUseHelper.rollUp}"
						reRender="frmLandUse"
						rendered="#{selectLandUseHelper.currentLevel gt 0}">
					</a4j:commandLink>
				</rich:toolBarGroup>
			</rich:toolBar>	
			
			<br />
			<br />
			
			<div style="width:100%;height:420px;overflow:auto;">
				<rich:dataTable id="dtLandUse" var="lu" value="#{selectLandUseHelper.landUseList}" rows="15" width="100%" columnsWidth="10%,70%,20%">
					<rich:column>
						<f:facet name="header">
							<h:outputText value="Codigo" />
						</f:facet>
						<h:outputText value="#{lu.completeCode}" />
					</rich:column>
					<rich:column>
						<f:facet name="header">
							<h:outputText value="Nombre" />
						</f:facet>
						<a4j:commandLink
							value="#{lu.name}"
							action="#{selectLandUseHelper.drillDown(lu)}"
							reRender="frmLandUse"
							rendered="#{not lu.leaf}">
						</a4j:commandLink>
						<h:outputText value="#{lu.name}" rendered="#{lu.leaf}" />
					</rich:column>
					<rich:column>
						<f:facet name="header">
							<h:outputText value="Incompatibilidades" />
						</f:facet>
						<a4j:commandLink
							value="#{editIncompatibilities ? 'Editar' : 'Ver'}"
							action="#{selectLandUseHelper.select(lu)}"
							reRender="incompatibilityDlg"
							oncomplete="#{rich:component('incompatibilityDlg')}.show()">
						</a4j:commandLink>
					</rich:column>
					<rich:column rendered="#{not editIncompatibilities}">
						<a4j:commandLink
							value="Seleccionar!"
							rendered="#{selectLandUseHelper.landUseNameInputFieldName.equals('')}"
							action="#{selectLandUseHelper.select(lu)}"
							reRender="#{selectLandUseHelper.reRenderComponents}"
							oncomplete="if (#{facesContext.maximumSeverity == null}) #{rich:component('selectLandUsePanel')}.hide(); else #{rich:component('msgPanel')}.show();">
						</a4j:commandLink>
						<a4j:commandLink
							value="Seleccionar"
							rendered="#{not selectLandUseHelper.landUseNameInputFieldName.equals('')}"
							action="#{selectLandUseHelper.select(lu)}"
							reRender="funCompleteSelection,#{selectLandUseHelper.reRenderComponents}"
							oncomplete="completeSelection()">
						</a4j:commandLink>
					</rich:column>
				</rich:dataTable>
			</div>
		</h:panelGroup>
		<br/>
	</h:form>
	
	<rich:modalPanel id="incompatibilityDlg">
		<f:facet name="header">
			<h:outputText value="Incompatibilidades de #{generalHelper.landUseName(selectLandUseHelper.selected)}"/>
		</f:facet>
		
		<f:facet name="controls">
			<h:panelGroup>
				<h:graphicImage value="/img/close.png" styleClass="hidelink"
					id="hideIncompatibilityDlg" />
				<rich:componentControl for="incompatibilityDlg" attachTo="hideIncompatibilityDlg"
					operation="hide" event="onclick" />
			</h:panelGroup>
		</f:facet>
		
		<div align="center">
			<h:form rendered="#{editIncompatibilities}">
				<a4j:commandLink value="Agregar"
					reRender="frmSimpleSelectLandUse"
					oncomplete="#{rich:component('simpleSelectLandUsePanel')}.show()">
					<f:setPropertyActionListener target="#{simpleLandUseHelper.reRenderComponents}" value="incompatibilityTable" />
					<f:setPropertyActionListener target="#{simpleLandUseHelper.addingToList}" value="#{true}" />
					<f:setPropertyActionListener target="#{simpleLandUseHelper.addToListMethod}" value="selectLandUseHelper.addIncompatibility" />
					<f:setPropertyActionListener target="#{simpleLandUseHelper.returnValueField}" value="selectLandUseHelper.pickedLandUse" />
				</a4j:commandLink>
				<br />
				<br />
				<rich:dataTable id="incompatibilityTable" 
					width="95%"
					rendered="#{not empty selectLandUseHelper.selected.incompatibleLandUses}"
					value="#{selectLandUseHelper.selected.incompatibleLandUses}"
					var="lu">
					<rich:column>
						<f:facet name="header">
							<h:outputText value="Codigo" />
						</f:facet>
						<h:outputText value="#{lu.completeCode}" />
					</rich:column>
					<rich:column>
						<f:facet name="header">
							<h:outputText value="Nombre" />
						</f:facet>
						<h:outputText value="#{lu.name}" />
					</rich:column>
					<rich:column rendered="#{editIncompatibilities}">
						<a4j:commandLink value="Eliminar" action="#{selectLandUseHelper.deleteIncompatibility}" reRender="incompatibilityTable">
							<f:setPropertyActionListener target="#{selectLandUseHelper.pickedLandUse}" value="#{lu}" />
						</a4j:commandLink>
					</rich:column>
				</rich:dataTable>
				
				<h:panelGroup rendered="#{empty selectLandUseHelper.selected.incompatibleLandUses}">
					<h:outputText value="No se encontraron incompatibilidades" />
				</h:panelGroup>
			</h:form>
		</div>
	</rich:modalPanel>
	
	<ui:include src="/WEB-INF/includes/util/simpleSelectLandUse.xhtml" />

</ui:component>
